package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.ArticleTagDao;
import com.clz.hyperit.entity.Article;
import com.clz.hyperit.entity.ArticleTag;
import com.clz.hyperit.entity.QuestionTag;
import com.clz.hyperit.entity.User;
import com.clz.hyperit.vo.PageInfo;
@Repository("articleTagDao")
public class ArticleTagDaoImpl extends BaseDaoImpl<ArticleTag> implements ArticleTagDao {

	@SuppressWarnings("unchecked")
	public List<ArticleTag> findByArticleId(Integer id) {
		String hql = "from ArticleTag where article_id = :id order by createtime desc";
		return getCurrentSession().createQuery(hql)
				.setInteger("id", id).list();
	}

	public void deleteByArticleID(Integer article_id) {
		String hql = "delete from ArticleTag where article_id = :article_id";
		 getCurrentSession().createQuery(hql).setInteger("article_id", article_id)
				.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public List<ArticleTag> getByTagid(Integer tag_id,PageInfo pageInfo) {
		String hql = " from ArticleTag where tag_id = :tag_id order by createtime desc";
		List<ArticleTag> list =  getCurrentSession().createQuery(hql)
				.setInteger("tag_id", tag_id)
				.setFirstResult(
						(pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())
				.setMaxResults(pageInfo.getSize()).list();
		// 获得表中总的条目数目
		String hql2 = "select count(*) from ArticleTag where tag_id = :tag_id";
		Object uniqueResult = getCurrentSession().createQuery(hql2)
				.setInteger("tag_id", tag_id)
				.uniqueResult();
		Long intValue = (new Integer(uniqueResult.toString())).longValue();
		pageInfo.setTotalCount(intValue);
		return list;
			
	}


}
